package com.joinlink.omiis.commons.persistence.util;

import java.io.File;
import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.joinlink.omiis.commons.persistence.preferences.IPreferenceKeys;

public class ConfigUtils {

	private static Logger logger = LoggerFactory.getLogger(ConfigUtils.class);

	public static void main(String[] args) {
		LoggerInitialize.initializeLogger();
		System.out.println(loadConfig(new File("./conf/jdbc.properties")));

	}

	public static Properties loadConfig(File configFile) {
		logger.info("开始加载：" + configFile.getName() + "的配置......");
		PropertiesConfiguration pc = new PropertiesConfiguration();
		if (!configFile.exists()) {
			throw new RuntimeException("配置文件：" + configFile.getAbsolutePath() + "不存在！");
		}

		try {
			pc.load(configFile);
		} catch (ConfigurationException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		Properties prop = new Properties();
		for (Iterator<String> it = pc.getKeys(); it.hasNext();) {
			String tmpKey = it.next();
			prop.put(tmpKey, pc.getString(tmpKey));
		}
		if (logger.isDebugEnabled()) {
			logger.debug("配置文件：" + configFile.getName() + "信息：" + prop);
		}
		logger.info("配置文件" + configFile.getName() + "加载完成。");
		return prop;
	}

}
